package org.brisling.domain.common;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.brisling.domain.sysman.inf.BizBeanDataModeler;
import org.brisling.domain.sysman.inf.ManageBeanDataModeler;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 用于多对多关系的关联表基类
 * @author Administrator
 *
 * @param <T> 主导对象
 * @param <E> 资源对象
 */
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

public class BaseRelationDomain<T extends BizBeanDataModeler,E extends BizBeanDataModeler> implements ManageBeanDataModeler<T,E> {
	
	@Transient
	@JsonIgnore
	private Class<T> clazzt;
	
	@Transient
	@JsonIgnore
	private Class<E> clazze;
	
	public BaseRelationDomain(Class<T> t,Class<E> e){
		clazzt = t;
		clazze = e;
	}
	
	@SuppressWarnings("unchecked")
	public BaseRelationDomain(T t, E e){
		clazzt = (Class<T>) t.getClass();
		clazze = (Class<E>) e.getClass();
		this.bizt = t;
		this.bize = e;
	}
	@Id
	@Column(name="id")
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@ManyToOne(cascade=CascadeType.REFRESH,fetch = FetchType.LAZY)
	@JoinColumn(name="biztid")
	@JsonIgnore
	private T bizt;
	
	@Column
	private String biztName;
	
	@Column(name="biztid",insertable=false,updatable=false)
	private Long biztid;
	
	@ManyToOne(cascade=CascadeType.REFRESH,fetch = FetchType.LAZY)
	@JoinColumn(name="bizeid")
	@JsonIgnore
	private E bize;	
	
	@Column(name="bizeid",insertable=false,updatable=false)
	private Long bizeid;
	
	@Column
	private String bizeName;
	
	@Column
	private Date createTime;	
	
	
	@Column(name="comments")
	private String comment;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public T getBizt() {
		return bizt;
	}

	public void setBizt(T _bizt) {
		this.bizt = _bizt;
		if(_bizt.getName()!=null)
			this.biztName = _bizt.getName();
		
		if(_bizt.getId()!=null)
			this.biztid = _bizt.getId();
	}

	public E getBize() {
		return bize;
	}

	public void setBize(E _bize) {
		this.bize = _bize;
		
		if(_bize.getName()!=null)
			this.bizeName = _bize.getName();
		
		if(_bize.getId()!=null)
			this.bizeid = _bize.getId();
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getBiztid() {
		return biztid;
	}

	public void setBiztid(Long _biztid) {
		this.biztid = _biztid;
		if(_biztid!=null){
			if(bizt==null){
				try {
					bizt = clazzt.newInstance();						
				} catch (InstantiationException | IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			bizt.setId(_biztid);
		}
		
	}

	public Long getBizeid() {
		return bizeid;
	}

	public void setBizeid(Long _bizeid) {
		this.bizeid = _bizeid;
		if(_bizeid!=null){
			if(bize==null){
				try {
					bize = clazze.newInstance();						
				} catch (InstantiationException | IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			bize.setId(_bizeid);
		}
	}

	public String getBiztName() {
		return biztName!=null?biztName:null;
		
	}

	public void setBiztName(String biztName) {
		this.biztName = biztName;
	}

	public String getBizeName() {
		return bizeName!=null?bizeName:null;
	}

	public void setBizeName(String bizeName) {
		this.bizeName = bizeName;
	}
	
	

}
